<script setup lang="ts">
import { download, get, loadFile, message, post } from '@/utils'
import Modal from './components/menu/Modal.vue'
import * as icons from '@arco-design/web-vue/es/icon'
import useModal from '@/hooks/useModal'
import { BtnPermission } from '@/enum/permission'
import useTable from '@/hooks/useTable'
const { store } = useModal()

const { formatColumns } = useTable()
const searchQuery = reactive({})
const formRef = ref()
const actionRef = ref()

const pagination = {
  showPageSize: true,
  pageSize: 20,
}

const request = (params: any, sort: any, filters: any): Promise<any> => {
  return new Promise((resolve, reject) => {
    const query = { ...params, size: params.pageSize }
    Reflect.deleteProperty(query, 'pageSize')
    get(
      {
        url: '/server-shop/sys-seller/seller/menu/lazy-list',
        data: query,
      },
      ({ data, code }) => {
        resolve({
          data: data,
          total: data.length,
          success: code == 200,
        })
      }
    )
  })
}
const columns = formatColumns([
  {
    title: '菜单图标',
    dataIndex: 'source',
    width: 100,
    render: ({ record }) => {
      if (record.source) return h(icons[record.source], { size: 20 })
      else return h('span', null, ' ')
    },
  },
  { title: '菜单名称', dataIndex: 'name', hideInSearch: false, width: 200 },
  { title: '路由地址', dataIndex: 'path', width: 200, ellipsis: true },

  { title: '菜单编号', dataIndex: 'code', hideInSearch: false, width: 200, ellipsis: true },
  { title: '备注', dataIndex: 'remark', width: 200 },
  { title: '排序值', dataIndex: 'sort', sorter: true, width: 200 },
  {
    title: '操作',

    dataIndex: 'operations',
    width: 200,
    fixed: 'right',
    btnList: [
      {
        name: '编辑',
        permission: BtnPermission.编辑,
        handle: ({ record }) => store.handleShow(record, true),
      },
      {
        name: '新增子项',
        permission: BtnPermission.新建,
        hidden: ({ record }) => record.category != 1,
        handle: ({ record }) => store.handleShow(record),
      },
      {
        name: '删除',
        type: 'popconfirm',
        permission: BtnPermission.删除,
        tip: '确认删除该数据吗?',
        handle: ({ record }) => {
          post({ url: '/server-shop/sys-seller/seller/menu/remove?ids=' + record.id }, ({ code, msg }) => {
            if (code == 200) actionRef.value.reload()
            message({
              type: code == 200 ? 'success' : 'error',
              content: msg,
            })
          })
        },
      },
    ],
  },
])
</script>

<template>
  <div class="page">
    <ProTable :columns="columns" :request="request" row-key="id" :form-ref="ref => (formRef = ref)" :pagination="pagination" header-title="" column-resizable :scroll="{ x: 1000 }" :params="searchQuery" :bordered="{ cell: true }" :action-ref="ref => (actionRef = ref)">
      <template #option-render="{ dom, reset }">
        <component :is="dom[1]" />
        <component :is="dom[0]" />
        <a-button type="primary" @click="store.handleShow({})" v-permission="[BtnPermission.新建]">
          <template #icon>
            <icon-plus />
          </template>
          新建</a-button
        >
      </template>
    </ProTable>

    <Modal v-if="store.show" v-model:show="store.show" :id="store.id" :edit="store.edit" @confirm="actionRef.reload" />
  </div>
</template>
